<?php

namespace app\http\middleware;

use think\facade\Request;

class CrossMiddleware
{
    public function handle($request, \Closure $next)
    {
        header('Access-Control-Allow-Origin: ' . Request::server('HTTP_ORIGIN'));
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, admin-token, sessionId");
        if (strtoupper($request->method()) == "OPTIONS") {
            $ret_json = json([
                'code' => 0,
                'msg' => 'success',
                'data' => ['CrossMiddleware' => 'success']
            ]);
            return $ret_json;
        }
        return $next($request);
    }
}
